package semant.firstpass;

import semant.Env;
import symbol.MethodInfo;
import symbol.Symbol;
import symbol.VarInfo;
import syntaxtree.Formal;
import syntaxtree.VisitorAdapter;
import util.List;

public class FormalHandler extends VisitorAdapter
{
	private Env e;
	private MethodInfo mi;
		
	private FormalHandler(Env e, MethodInfo mi)
	{
		this.e = e;
		this.mi = mi;
	}
	
	public static void firstPass(Env e, MethodInfo mi, List<Formal> formalList) 
	{
		FormalHandler h = new FormalHandler(e, mi);
		
		for (List<Formal> l = formalList; l != null; l = l.tail)
			l.head.accept(h);			
	}
	
	@Override
	public void visit(Formal node)
	{
		Symbol s = Symbol.symbol(node.name.s);	
		
		VarInfo formal = new VarInfo(node.type, s);
		
		if (!mi.addFormal(formal))	
			e.err.Error(node, new Object[]{"Parâmetro de método redefinido.", "Método: " + mi.decorateName(), "Parâmetro: " + s});
	}
}
